from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer

from luckin.authentication import TokenAuthentication
from luckin.serialization import GoodsSerializers
from luckin.service.goods import handle_goods


class GoodsView(APIView):
    http_method_names = ["get"]

    def get(self, request):
        response_data = {
            "code": 200,
            "message": "success"
        }
        flag, data = handle_goods(request)
        if flag:
            goods_ser = GoodsSerializers(data,many=True)
            response_data["data"] = goods_ser.data
        else:
            response_data["code"] = 400
            response_data["message"] = data
        return Response(status=200, content_type="application/json", data=response_data)
